package com.github.tommas.admintpl.controller;

import com.github.tommas.admintpl.bean.model.User;
import com.github.tommas.admintpl.bean.vo.AccountInfo;
import com.github.tommas.admintpl.common.CommonResult;
import com.github.tommas.admintpl.security.UserPrincipal;
import com.github.tommas.admintpl.service.AccountService;
import com.github.tommas.admintpl.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    AccountService accountService;

    @Autowired
    UserService userService;

    @GetMapping("/info")
    public CommonResult<AccountInfo> accountInfo() {
        Subject subject = SecurityUtils.getSubject();
        UserPrincipal userPrincipal = UserPrincipal.fromPrincipalCollection(subject.getPrincipals());

        User user = userService.findUser(userPrincipal.getUserId());
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setUser(user);
        accountInfo.setAuthorizationInfo(accountService.getUserAuthorizationInfo(user.getUsername(), user.getId()));

        return CommonResult.success(accountInfo);
    }
}
